﻿Imports System.Data.SqlClient
Public Class frmCheques
    Dim isDateValidate As Boolean = True
    Private Sub CheckBoxX1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chckBxIsnomin.CheckedChanged
        Try
            If chckBxIsnomin.Checked Then
                pnlPersonne.Enabled = True
                If frm.Name = frmPaiementParai.Name Then
                    txtNom.Text = DirectCast(frmMesParraines.cmbParrin.SelectedItem, DictionaryEntry).Value.ToString.Split(" ")(0)
                    If DirectCast(frmMesParraines.cmbParrin.SelectedItem, DictionaryEntry).Value.ToString.Split(" ").Count > 1 Then
                        txtPrenom.Text = DirectCast(frmMesParraines.cmbParrin.SelectedItem, DictionaryEntry).Value.ToString.Split(" ")(1)
                    End If
                ElseIf frm.Name = frmPaiementFour.Name Then
                    txtNom.Text = DirectCast(frmServicesFrn.cmbFour.SelectedItem, DictionaryEntry).Value.ToString.Split(" ")(0)
                    If DirectCast(frmServicesFrn.cmbFour.SelectedItem, DictionaryEntry).Value.ToString.Split(" ").Count > 1 Then
                        txtPrenom.Text = DirectCast(frmServicesFrn.cmbFour.SelectedItem, DictionaryEntry).Value.ToString.Split(" ")(1)
                    End If
                ElseIf frm.Name = Frm_Paiement_Fournisseurs.Name Then
                    txtNom.Text = Frm_Paiement_Fournisseurs.Comb_Nom_Fournisseurs.SelectedItem.ToString.Split(" ")(0)
                    txtPrenom.Text = Frm_Paiement_Fournisseurs.Comb_Nom_Fournisseurs.SelectedItem.ToString. _
                        Substring(txtNom.Text.Length + 2)
                    'ElseIf frm.Name = Frm_Paiement_Clients.Name Then
                    '    txtNom.Text = nomClient.Split(" ")(0)
                    '    If nomClient.Split(" ").Count > 1 Then
                    '        txtPrenom.Text = nomClient.Substring(txtNom.Text.Length + 2)
                    '    End If
                End If
            Else
                txtNom.Clear()
                txtPrenom.Clear()
                pnlPersonne.Enabled = False
                nomin = Nothing
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub

    Private Sub frmLiaiChquPay_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        frm.Enabled = True
        For Each c In frm.Controls
            If TypeOf c Is ListView Then
                For Each it As ListViewItem In CType(c, ListView).Items
                    it.Selected = False
                Next
                Exit For
            End If
        Next
    End Sub
    Private Sub btnAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouter.Click
        Try
            If cheq.idPai = 0 Then
                If isDateValidate And Not String.IsNullOrEmpty(txtNumCheq.Text) And Not String.IsNullOrWhiteSpace(txtNumCheq.Text) Then
                    cheq = New cheque
                    If mskdTxtDteRetra.Text = "  /  /" Then
                        cheq.dteRetra = Nothing
                    Else
                        cheq.dteRetra = mskdTxtDteRetra.Text
                    End If
                    With cheq
                        .numChe = txtNumCheq.Text
                        .dateChequ = dtpCheque.Value
                        .banqueDestinataire = cmbBnqDestinataire.SelectedItem.ToString
                        .banqueDestinateur = cmbBanqDestinateur.SelectedItem.ToString
                        .nominatif = chckBxIsnomin.Checked
                    End With



                    If chckBxIsnomin.Checked Then
                        If Not String.IsNullOrEmpty(txtNom.Text) And Not String.IsNullOrWhiteSpace(txtNom.Text) Then
                            nomin = New nominatif
                            With nomin
                                .nom = txtNom.Text
                                .prenom = txtPrenom.Text
                            End With
                        Else
                            MsgBox("Veuillez remplir tous les champs", MsgBoxStyle.Critical, "Erreur")
                            Exit Sub
                        End If
                    End If
                    MsgBox("Le Chéque est bien remplie", MsgBoxStyle.Information, "Succès")
                    'maVider(Me)
                    Me.Close()
                Else
                    MsgBox("La date ou le numéro de Chéque est incorrecte!!!", MsgBoxStyle.Critical, "Erreur")
                End If
            Else
                MsgBox("Le Chéque est déjà ajouter!!!", MsgBoxStyle.Critical, "Erreur")
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub
    Private Sub mskdTxtDteRetra_TypeValidationCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.TypeValidationEventArgs) Handles mskdTxtDteRetra.TypeValidationCompleted
        If (Not e.IsValidInput) Then
            If mskdTxtDteRetra.Text <> "  /  /" Then
                With SuperTooltip1
                    .DefaultTooltipSettings = New DevComponents.DotNetBar.SuperTooltipInfo("La date est incorrecte!!!", "", "la date doit être au format jj/mm/aaaa", Nothing, Nothing, DevComponents.DotNetBar.eTooltipColor.Blue)
                    .SetSuperTooltip(mskdTxtDteRetra, .DefaultTooltipSettings)
                    .ShowTooltip(mskdTxtDteRetra)
                End With
                isDateValidate = False
            End If

            If mskdTxtDteRetra.Text = "  /  /" Then
                isDateValidate = True
            End If
        Else
            isDateValidate = True
        End If
    End Sub

    Private Sub frmLiaiChquPay_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            'Remplir les deux combo des banques
            Dim banqueExist As Boolean
            Dim str As String
            Dim cmb As ComboBox
            For j As Integer = 0 To 1
                If j = 0 Then
                    str = "banqueDestinataire"
                    cmb = cmbBnqDestinataire
                Else
                    str = "banqueDestinateur"
                    cmb = cmbBanqDestinateur
                End If

                Cmd = New SqlCommand("select distinct " & str & " from virement", Con)
                open()
                Dr = Cmd.ExecuteReader
                If Dr.HasRows Then
                    While Dr.Read()
                        banqueExist = False
                        For i As Integer = 0 To cmbBnqDestinataire.Items.Count - 1
                            cmb.SelectedIndex = i
                            If cmb.Text = Dr.GetString(0) Then
                                banqueExist = True
                                Exit For
                            End If
                        Next
                        If Not banqueExist Then
                            cmbBnqDestinataire.Items.Add(Dr.GetString(0))
                        End If
                    End While
                End If
                Dr.Close()
                cnClose()
            Next
            cmbBnqDestinataire.SelectedIndex = 1
            cmbBanqDestinateur.SelectedIndex = 0
            'End

            If Not IsNothing(cheq.numChe) Then
                txtNumCheq.Text = cheq.numChe
                If Format(cheq.dteRetra, "dd/MM/yyyy") <> "01/01/1900" And Format(cheq.dteRetra, "dd/MM/yyyy") <> "01/01/0001" Then
                    mskdTxtDteRetra.Text = cheq.dteRetra
                End If
                dtpCheque.Value = Format(cheq.dateChequ, "dd/MM/yyyy")
                cmbBanqDestinateur.Text = cheq.banqueDestinateur
                cmbBnqDestinataire.Text = cheq.banqueDestinataire
            Else
                btnModifier.Enabled = False
            End If

            If Not IsNothing(nomin.nom) Then
                chckBxIsnomin.Checked = True
                txtNom.Text = nomin.nom
                txtPrenom.Text = nomin.prenom
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub

    Private Sub btnModifier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifier.Click
        Try
            If isDateValidate And Not String.IsNullOrEmpty(txtNumCheq.Text) And Not String.IsNullOrWhiteSpace(txtNumCheq.Text) Then
                If mskdTxtDteRetra.Text = "  /  /" Then
                    cheq.dteRetra = Nothing
                Else
                    cheq.dteRetra = mskdTxtDteRetra.Text
                End If
                Cmd = New SqlCommand("UPDATE [Salle_Mariage].[dbo].[cheques] " &
                                        "SET [numChe] = @numChe " &
                                        ",[nominatif] = " & convertToBit(chckBxIsnomin.Checked) &
                                        ",[dteRetra] = " & nullPourDate(cheq.dteRetra) &
                                        ",[dateChequ] = '" & Format(dtpCheque.Value, "MM/dd/yyyy") &
                                        "',[banqueDestinataire] = @banqueDestinataire " &
                                        ",[banqueDestinateur] = @banqueDestinateur " &
                                        "WHERE idPai = " & cheq.idPai, Con)
                Cmd.Parameters.Add("@numChe", SqlDbType.VarChar)
                Cmd.Parameters("@numChe").Value = txtNumCheq.Text

                Cmd.Parameters.Add("@banqueDestinataire", SqlDbType.VarChar)
                Cmd.Parameters("@banqueDestinataire").Value = cmbBnqDestinataire.SelectedItem.ToString

                Cmd.Parameters.Add("@banqueDestinateur", SqlDbType.VarChar)
                Cmd.Parameters("@banqueDestinateur").Value = cmbBanqDestinateur.SelectedItem.ToString
                open()
                Cmd.ExecuteNonQuery()

                If chckBxIsnomin.Checked Then
                    If Not IsNothing(nomin.idPai) And nomin.idPai <> 0 Then
                        If nomin.nom <> txtNom.Text And nomin.prenom <> txtPrenom.Text Then
                            If Not String.IsNullOrEmpty(txtNom.Text) And Not String.IsNullOrWhiteSpace(txtNom.Text) Then
                                If cheq.nominatif Then
                                    Cmd.CommandText = "UPDATE [Salle_Mariage].[dbo].[nominatifA] " &
                                                    "SET [nom] = @nom,[Prenom] = @Prenom " &
                                                    "WHERE idNomin = " & nomin.idNomin
                                    Cmd.Parameters.Add("@nom", SqlDbType.VarChar)
                                    Cmd.Parameters("@nom").Value = txtNom.Text
                                    Cmd.Parameters.Add("@Prenom", SqlDbType.VarChar)
                                    Cmd.Parameters("@Prenom").Value = txtPrenom.Text
                                    Cmd.ExecuteNonQuery()
                                    With nomin
                                        .nom = txtNom.Text
                                        .prenom = txtPrenom.Text
                                    End With
                                End If
                            End If
                        End If
                    Else
                        Dim idNomin As Integer 'pour stocker la valeur de l'idNominatif ajouté

                        Cmd.Parameters.Clear()
                        'cette partie ajoute le nominatif et retourne l'id de ce dernier
                        nomin = New nominatif
                        With nomin
                            .nom = txtNom.Text
                            .prenom = txtPrenom.Text
                        End With

                        Cmd.CommandText = "ajouterNominatif"
                        Cmd.CommandType = CommandType.StoredProcedure
                        Cmd.Parameters.Add("@nom", SqlDbType.VarChar)
                        Cmd.Parameters("@nom").Value = nomin.nom
                        Cmd.Parameters.Add("@Prenom", SqlDbType.VarChar)
                        Cmd.Parameters("@Prenom").Value = nomin.prenom
                        Cmd.Parameters.Add("@idNomin", SqlDbType.Int)
                        Cmd.Parameters("@idNomin").Direction = ParameterDirection.Output
                        Cmd.ExecuteNonQuery()
                        idNomin = Cmd.Parameters("@idNomin").Value

                        If Not IsNothing(nomin.nom) Then
                            Cmd.CommandText = "insert into nominCheq values(" & cheq.idPai & "," & idNomin & ")"
                            Cmd.ExecuteNonQuery()
                        End If
                    End If
                Else
                    If Not IsNothing(nomin.idPai) And nomin.idPai <> 0 Then
                        Cmd.CommandText = "DELETE FROM [nominatifA] WHERE idNomin = " & nomin.idNomin
                        Cmd.ExecuteNonQuery()
                        nomin = Nothing
                    End If
                End If
                MsgBox("Le chéque a été bien modifié", MsgBoxStyle.Information, "Succés")
                cnClose()
            Else
                MsgBox("La date ou le numéro de Chéque est incorrecte!!!", MsgBoxStyle.Critical, "Erreur")
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub
End Class